//
//  ProductOperationCell.m
//  FactorySale
//
//  Created by 胡小康 on 2020/4/21.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "ProductOperationCell.h"
#import "SYBigImage.h"
@interface ProductOperationCell()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;// 名称
@property (weak, nonatomic) IBOutlet UILabel *totalCountLabel;// 累计申请数

@property (nonatomic,strong)NSString *partsId;
@property (nonatomic,strong)NSString *attribute;
@end
@implementation ProductOperationCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.confirmButton.userInteractionEnabled = NO;
    [self.confirmButton setBackgroundColor:[UIColor colorWithHexString:@"#CFCFCF"]];
    self.numberTextField.delegate = self;
    [self.numberTextField addTarget:self action:@selector(numberChange) forControlEvents:UIControlEventEditingChanged];
}
+ (instancetype)cellWithTableView:(UITableView *)tableView {
    
    static NSString *ID = @"ProductOperationCell";
    
    ProductOperationCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        
        cell = [NSBundle.mainBundle loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].firstObject;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)setModel:(CooperationPriductListModel *)model {
    
    __weak typeof(self) _weakSelf = self;
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.attributeLogo] placeholderImage:imgname(GOODS_PLACE_HOLDER) completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if (image && cacheType == SDImageCacheTypeNone) {
            _weakSelf.iconImageView.alpha = 0;
            [UIView animateWithDuration:1.0 animations:^{
                _weakSelf.iconImageView.alpha = 1.0f;
            }];
        } else {
            _weakSelf.iconImageView.alpha = 1.0f;
        }
    }];
    
    SYBigImage * bigI = [[SYBigImage alloc]init];
    [self.iconImageView addGestureRecognizer:bigI];
    self.iconImageView.userInteractionEnabled = YES;
    self.partsId = model.partsId;
    self.attribute = model.id;
    self.nameLabel.text = [NSString stringWithFormat:@"%@",model.name];
    self.totalCountLabel.text = [NSString stringWithFormat:@"累计申请数:%@个",model.appCount];
    self.numberTextField.text = model.count;
    if (self.numberTextField.text.length>0) {
        self.confirmButton.userInteractionEnabled = YES;
        [self.confirmButton setBackgroundColor:[UIColor colorWithHexString:@"#31CEA9"]];
    } else {
        self.confirmButton.userInteractionEnabled = NO;
        [self.confirmButton setBackgroundColor:[UIColor colorWithHexString:@"#CFCFCF"]];
    }

}
- (void)numberChange {
    if (self.numberTextField.text.length>0 && self.numberTextField.text.intValue >0 ) {
        self.confirmButton.userInteractionEnabled = YES;
        [self.confirmButton setBackgroundColor:[UIColor colorWithHexString:@"#31CEA9"]];
    } else {
        self.confirmButton.userInteractionEnabled = NO;
        [self.confirmButton setBackgroundColor:[UIColor colorWithHexString:@"#CFCFCF"]];
    }
    if (self.numberTextField.text.length >=8) {
        self.numberTextField.text = [self.numberTextField.text substringToIndex:8];
    }
    if (self.countChangedClick) {
        self.countChangedClick(self.numberTextField.text);
    }
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    NSArray *attt = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];
    if (![attt containsObject:string]  && ![string  isEqualToString:@""]) {
        ShowToastWithText(@"只能输入数字");
        return NO;
    }else{
        return YES;
    }
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
